package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;


    @Autowired
    private DishMapper dishMapper;


    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //获取到用户id并设置到数据库
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询购物车里面数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.shoppSelectById(shoppingCart);
        if (shoppingCartList != null && shoppingCartList.size() == 1) {
            //如果菜品或者套餐存在的话数量进行加一的操作
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            //进行修改这里只是内存进行修改,被没有真改
            shoppingCartMapper.shoppUpdateNumber(shoppingCart);
        } else {
            //说明第一进购物车
            //我们这么判断是菜品还是套餐
            Long dishId = shoppingCart.getDishId();
            if (dishId != null) {
                //我们通过菜品id查询到菜品信息并存入数据库
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
            } else {
                Long setmealId = shoppingCart.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            //统一设置公共字段
            shoppingCart.setCreateTime(LocalDateTime.now());
            //进行添加操作
            shoppingCartMapper.insert(shoppingCart);
        }
    }


    @Override
    public List<ShoppingCart> list() {
        return shoppingCartMapper.shoppSelectById(ShoppingCart.builder()
                .userId(BaseContext.getCurrentId()).build());
    }


    @Override
    public void delete() {
        Long id = BaseContext.getCurrentId();
        shoppingCartMapper.delete(id);
    }


    @Override
    public void deleteById(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //获取到用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //根据用户名查到购物车数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.shoppSelectById(shoppingCart);
        if (shoppingCartList != null && shoppingCartList.size() == 1) {
            //需要修改数量减 1
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.shoppUpdateNumber(shoppingCart);
        }
        shoppingCartList.forEach(f -> {
            if (f.getNumber() == 0) {
                shoppingCartMapper.deleteDish(f.getDishId());
            } else {
                shoppingCartMapper.deleteSetmeal(f.getSetmealId());
            }
        });
    }
}
